/**  
* <p>Title: SecurityAreaService.java</p >  
* <p>Description: </p >  
* <p>Company:luo.com</p >  
* @author 魏家洛 
* @date 2019年1月22日  
* @version 1.0  
*/  
package com.pmsystem.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pmsystem.dao.AccidentMapper;
import com.pmsystem.dao.SecurityAreaMapper;
import com.pmsystem.dao.SecurityManMapper;
import com.pmsystem.dao.SecurityMisMapper;
import com.pmsystem.pojo.Accident;
import com.pmsystem.pojo.SecurityArea;
import com.pmsystem.pojo.SecurityMan;
import com.pmsystem.pojo.SecurityMis;
import com.pmsystem.util.BeanUtil;

/**  
* <p>Title: SecurityAreaService</p >  
* <p>Description: </p >  
* @author 魏家洛  
* @date 2019年1月22日  
*/
@Service
public class SecurityService {
	@Autowired
	SecurityAreaMapper securityAreaMapper;
	@Autowired
	SecurityManMapper securityManMapper;
	@Autowired
	SecurityMisMapper securityMisMapper;
	@Autowired
	AccidentMapper accidentMapper;
	
	//安保区域
	/**  
	 * <p>Title: findAreaPage</p >  
	 * <p>Description: 传入页码和页大小和查询条件获取页码对应安保人员信息</p >  
	 * @author 魏家洛  
	 */
	public Map findAreaPage(Integer pageNo,Integer pageSize,String condition) {
		if(!BeanUtil.isValid(condition))
			condition=null;
		Map map = new HashMap<>();
		int pageNum = (int)Math.ceil(securityAreaMapper.selectAreaCount(condition)/(double)pageSize);
		map.put("pageNum",pageNum);
		int start = (pageNo-1)*pageSize;
		map.put("areaPage",securityAreaMapper.selectAreaPage(start,pageSize,condition));
		return map;
	}
	/**  
	 * <p>Title: deleteArea</p >  
	 * <p>Description: 删除安保区域信息</p >  
	 * @author 魏家洛
	 */  
	public boolean deleteArea(Integer id) {
		int row = securityAreaMapper.deleteAreaById(id);
		if(row==1) 
			return true;
		else
			return false;
	}
	/**
	 * <p>Title: selectAreaById</p >  
	 * <p>Description:通过安保区域id查找安保区域信息 </p >  
	 * @author 魏家洛
	 * @param id
	 * @return
	 */
	public SecurityArea selectArea(Integer id) {
		return securityAreaMapper.selectAreaById(id);
	}
	/**  
	 * <p>Title: updateArea</p >  
	 * <p>Description:更新安保区域信息</p >  
	 * @author 魏家洛
	 * @param securityArea  
	 */  
	public boolean updateArea(SecurityArea securityArea) {
		return (securityAreaMapper.updateAreaByArea(securityArea)==1);
	}
	/**  
	 * <p>Title: addArea</p >  
	 * <p>Description:添加安保区域信息 </p >  
	 * @author 魏家洛
	 * @param securityAreaName  
	 */  
	public boolean addArea(String securityAreaName) {
		return(securityAreaMapper.addArea(securityAreaName)==1);
	}
	
	
	//安保人员
	/**  
	 * <p>Title: findManPage</p >  
	 * <p>Description: </p >  
	 * @author 魏家洛
	 * @param pageNo
	 * @param pageSize
	 * @return  
	 */  
	public Map findManPage(Integer pageNo, Integer pageSize,String condition) {
		if(!BeanUtil.isValid(condition))
			condition=null;
		Map map = new HashMap<>();
		int pageNum = (int)Math.ceil(securityManMapper.selectManCount(condition)/(double)pageSize);
		map.put("pageNum",pageNum);
		int start = (pageNo-1)*pageSize;
		map.put("manPage",securityManMapper.selectManPage(start,pageSize,condition));
		return map;
	}
	/**  
	 * <p>Title: deleteMan</p >  
	 * <p>Description:删除安保人员信息 </p >  
	 * @author 魏家洛
	 * @param id  
	 */  
	public boolean deleteMan(Integer id) {
		int row = securityManMapper.deleteManById(id);
		if(row==1) 
			return true;
		else
			return false;
	}
	/**  
	 * <p>Title: selectMan</p >  
	 * <p>Description: 通过安保人员id查找安保人员信息</p >  
	 * @author 魏家洛
	 * @param id
	 * @return  
	 */  
	public SecurityMan selectMan(Integer id) {
		return securityManMapper.selectManById(id);
	}
	/**  
	 * <p>Title: updateMan</p >  
	 * <p>Description:更新安保人员信息 </p >  
	 * @author 魏家洛
	 * @param securityMan  
	 */  
	public boolean updateMan(SecurityMan securityMan) {
		return (securityManMapper.updateManByMan(securityMan)==1);
	}
	/**  
	 * <p>Title: addMan</p >  
	 * <p>Description:添加安保人员信息 </p >  
	 * @author 魏家洛
	 * @param securityMan  
	 */  
	public boolean addMan(SecurityMan securityMan) {
		return(securityManMapper.addMan(securityMan)==1);
	}
	
	
	
	//安保任务
	/**  
	 * <p>Title: findMisPage</p >  
	 * <p>Description: </p >  
	 * @author 魏家洛
	 * @param pageNo
	 * @param pageSize
	 * @return  
	 */  
	public Map findMisPage(Integer pageNo, Integer pageSize,String condition,String startTime,String endTime) {
		if(!BeanUtil.isValid(condition))
			condition=null;
		if(!BeanUtil.isValid(startTime))
			startTime=null;
		if(!BeanUtil.isValid(endTime))
			endTime=null;
		Map map = new HashMap<>();
		int pageNum = (int)Math.ceil(securityMisMapper.selectMisCount(condition,startTime,endTime)/(double)pageSize);
		map.put("pageNum",pageNum);
		int start = (pageNo-1)*pageSize;
		map.put("misPage",securityMisMapper.selectMisPage(start,pageSize,condition,startTime,endTime));
		return map;
	}
	/**  
	 * <p>Title: deleteMis</p >  
	 * <p>Description:删除安保任务信息 </p >  
	 * @author 魏家洛
	 * @param id  
	 */  
	public boolean deleteMis(Integer id) {
		int row = securityMisMapper.deleteMisById(id);
		if(row==1) 
			return true;
		else
			return false;
	}
	/**  
	 * <p>Title: selectMis</p >  
	 * <p>Description: 通过安保任务id查找安保任务信息</p >  
	 * @author 魏家洛
	 * @param id
	 * @return  
	 */  
	public SecurityMis selectMis(Integer id) {
		return securityMisMapper.selectMisById(id);
	}
	/**  
	 * <p>Title: updateMis</p >  
	 * <p>Description:更新安保任务信息 </p >  
	 * @author 魏家洛
	 * @param securityMis
	 */  
	public boolean updateMis(SecurityMis securityMis) {
		return (securityMisMapper.updateMisByMis(securityMis)==1);
	}
	/**  
	 * <p>Title: addMis</p >  
	 * <p>Description:添加安保任务信息 </p >  
	 * @author 魏家洛
	 * @param securityMis 
	 */  
	public boolean addMis(SecurityMis securityMis) {
		if(securityAreaMapper.selectAreaById(securityMis.getSecurityAreaId())==null &&
		   securityManMapper.selectManById(securityMis.getSecurityManId())==null)
			return false;
		else
			return(securityMisMapper.addMis(securityMis)==1);
	}
	
	
	
	
	//重大事件
	/**  
	 * <p>Title: findAccidentPage</p >  
	 * <p>Description:传入页码和页大小获取页码对应安保人员信息</p >  
	 * @author 魏家洛
	 * @param pageNo
	 * @param pageSize
	 * @return  
	 */  
	public Map findAccidentPage(Integer pageNo, Integer pageSize,String condition,String time) {
		if(!BeanUtil.isValid(condition))
			condition=null;
		if(!BeanUtil.isValid(time))
			time=null;
		Map map = new HashMap<>();
		int pageNum = (int)Math.ceil(accidentMapper.selectAccidentCount(condition,time)/(double)pageSize);
		map.put("pageNum",pageNum);
		int start = (pageNo-1)*pageSize;
		map.put("accPage",accidentMapper.selectAccidentPage(start,pageSize,condition,time));
		return map;
	}
	/**  
	 * <p>Title: deleteaccident</p >  
	 * <p>Description:删除重大事件信息 </p >  
	 * @author 魏家洛
	 * @param id  
	 */  
	public boolean deleteAccident(Integer id) {
		int row = accidentMapper.deleteAccidentById(id);
		if(row==1) 
			return true;
		else
			return false;
	}
	/**  
	 * <p>Title: selectAccident</p >  
	 * <p>Description: 通过重大事件id查找重大事件信息</p >  
	 * @author 魏家洛
	 * @param id
	 * @return  
	 */  
	public Accident selectAccident(Integer id) {
		return accidentMapper.selectAccidentById(id);
	}
	/**  
	 * <p>Title: updateAccident</p >  
	 * <p>Description:更新重大事件信息 </p >  
	 * @author 魏家洛
	 * @param Accident
	 */  
	public boolean updateAccident(Accident accident) {
		return (accidentMapper.updateAccidentByAccident(accident)==1);
	}
	/**  
	 * <p>Title: addAccident</p >  
	 * <p>Description:添加重大事件信息 </p >  
	 * @author 魏家洛
	 * @param accident 
	 */  
	public boolean addAccident(Accident accident) {
		return(accidentMapper.addAccident(accident)==1);
	}
	/**  
	 * <p>Title: getProgress</p >  
	 * <p>Description:获取当天任务进度</p >  
	 * @author 魏家洛
	 * @return  
	 */  
	public int getProgress(String currentDate) {
		Float progress = securityMisMapper.getProgress(currentDate);
		if(progress==null)
			return 100;
		else
			return (int)(progress+0);
	}
}
